PACKAGE=vit
VERSION := $(shell rpm -q --qf '%{version}\n' --specfile $(PACKAGE).spec | head -1)
RELEASE := $(shell rpm -q --qf '%{release}\n' --specfile $(PACKAGE).spec | head -1)


ATI_OLD			= ATI
ATI_OLD_PACKAGE		= ati
ATI_OLD_SCRIPT		= install-ati.sh

ATI_NEW			= ATI 8X
ATI_NEW_PACKAGE		= ati_8-x
ATI_NEW_SCRIPT		= install-ati-8x.sh

NVIDIA_OLDEST		= NVIDIA 71xx
NVIDIA_OLDEST_PACKAGE	= nvidia_71xx
NVIDIA_OLDEST_SCRIPT	= install-nvidia_71xx.sh

NVIDIA_OLD		= NVIDIA 96xx
NVIDIA_OLD_PACKAGE	= nvidia_96xx
NVIDIA_OLD_SCRIPT	= install-nvidia_96xx.sh

NVIDIA_NEW		= NVIDIA 169.xx
NVIDIA_NEW_PACKAGE	= nvidia_169.xx
NVIDIA_NEW_SCRIPT	= install-nvidia_169.xx.sh


TEXTDOMAIN   = ${PACKAGE}

RPM          = /usr/bin/rpm

PRODUCT      = "PCLinuxOS 2007"
export PRODUCT

MSGFMT       = /usr/bin/msgfmt


bindir=/usr/bin
sbindir=/usr/sbin
datadir=/usr/share
localedir=$(datadir)/locale


INSTLOCALES := ${patsubst l10n/%.po,%,${wildcard l10n/*.po}}
TMPPOS := ${wildcard l10n/*.po.tmp}
POS := ${wildcard l10n/*.po}
TMPPOS := ${patsubst %,%.tmp,${POS}}
MOS := ${patsubst %.po,%.mo,${POS}}

.PHONY: all clean installdirs install cleandist distdir localcopy tar rpm rpm-install ${INSTLOCALES}

all: vit ${MOS} pclinuxos-vit.desktop

vit: vit.in
	sed \
	-e 's/%ATI_OLD%/$(ATI_OLD)/g' \
	-e 's/%ATI_OLD_PACKAGE%/$(ATI_OLD_PACKAGE)/g' \
	-e 's/%ATI_OLD_SCRIPT%/$(ATI_OLD_SCRIPT)/g' \
	-e 's/%ATI_NEW%/$(ATI_NEW)/g' \
	-e 's/%ATI_NEW_PACKAGE%/$(ATI_NEW_PACKAGE)/g' \
	-e 's/%ATI_NEW_SCRIPT%/$(ATI_NEW_SCRIPT)/g' \
	-e 's/%NVIDIA_OLDEST%/$(NVIDIA_OLDEST)/g' \
	-e 's/%NVIDIA_OLDEST_PACKAGE%/$(NVIDIA_OLDEST_PACKAGE)/g' \
	-e 's/%NVIDIA_OLDEST%/$(NVIDIA_OLDEST)/g' \
	-e 's/%NVIDIA_OLD%/$(NVIDIA_OLD)/g' \
	-e 's/%NVIDIA_OLD_PACKAGE%/$(NVIDIA_OLD_PACKAGE)/g' \
	-e 's/%NVIDIA_OLD_SCRIPT%/$(NVIDIA_OLD_SCRIPT)/g' \
	-e 's/%NVIDIA_NEW%/$(NVIDIA_NEW)/g' \
	-e 's/%NVIDIA_NEW_PACKAGE%/$(NVIDIA_NEW_PACKAGE)/g' \
	-e 's/%NVIDIA_NEW_SCRIPT%/$(NVIDIA_NEW_SCRIPT)/g' \
	$< > vit



pclinuxos-vit.desktop: pclinuxos-vit.desktop.in
	intltool-merge l10n/ -d -u -c l10n/.intltool-merge-cache $< $@

${TMPPOS}: l10n/%.po.tmp : l10n/%.po
	sed \
	-e 's/%ATI_OLD%/$(ATI_OLD)/g' \
	-e 's/%ATI_OLD_PACKAGE%/$(ATI_OLD_PACKAGE)/g' \
	-e 's/%ATI_OLD_SCRIPT%/$(ATI_OLD_SCRIPT)/g' \
	-e 's/%ATI_NEW%/$(ATI_NEW)/g' \
	-e 's/%ATI_NEW_PACKAGE%/$(ATI_NEW_PACKAGE)/g' \
	-e 's/%ATI_NEW_SCRIPT%/$(ATI_NEW_SCRIPT)/g' \
	-e 's/%NVIDIA_OLDEST%/$(NVIDIA_OLDEST)/g' \
	-e 's/%NVIDIA_OLDEST_PACKAGE%/$(NVIDIA_OLDEST_PACKAGE)/g' \
	-e 's/%NVIDIA_OLDEST%/$(NVIDIA_OLDEST)/g' \
	-e 's/%NVIDIA_OLD%/$(NVIDIA_OLD)/g' \
	-e 's/%NVIDIA_OLD_PACKAGE%/$(NVIDIA_OLD_PACKAGE)/g' \
	-e 's/%NVIDIA_OLD_SCRIPT%/$(NVIDIA_OLD_SCRIPT)/g' \
	-e 's/%NVIDIA_NEW%/$(NVIDIA_NEW)/g' \
	-e 's/%NVIDIA_NEW_PACKAGE%/$(NVIDIA_NEW_PACKAGE)/g' \
	-e 's/%NVIDIA_NEW_SCRIPT%/$(NVIDIA_NEW_SCRIPT)/g' \
	$< > $@

${MOS}: l10n/%.mo : l10n/%.po.tmp
	$(MSGFMT) -o $@ $<

installdirs:
	install -d -m 0755 $(prefix)/$(bindir)
	install -d -m 0755 $(prefix)/$(datadir)/license
	install -d -m 0755 $(prefix)/$(datadir)/mdk/backgrounds
	install -d -m 0755 $(prefix)/$(sbindir)
	install -d -m 0755 $(prefix)/$(datadir)/applications

${INSTLOCALES}: %: l10n/%.mo
	install -d -m 755 $(prefix)/$(localedir)/$@/LC_MESSAGES/
	install -m 644 $< $(prefix)/$(localedir)/$@/LC_MESSAGES/${TEXTDOMAIN}.mo

install: all installdirs ${INSTLOCALES}
	install -m 0755 vit $(prefix)/$(bindir)/vit
	install -m 0644 ati.html $(prefix)/$(datadir)/license/ati.html
	install -m 0644 nvidia.html $(prefix)/$(datadir)/license/nvidia.html
	install -m 0644 vit.jpg $(prefix)/$(datadir)/mdk/backgrounds/vit.jpg
	
	install -m 0755 $(ATI_NEW_SCRIPT) $(prefix)/$(sbindir)/
	install -m 0755 $(ATI_OLD_SCRIPT) $(prefix)/$(sbindir)/
	install -m 0755 $(NVIDIA_NEW_SCRIPT) $(prefix)/$(sbindir)/
	install -m 0755 $(NVIDIA_OLDEST_SCRIPT) $(prefix)/$(sbindir)/
	install -m 0755 $(NVIDIA_OLD_SCRIPT) $(prefix)/$(sbindir)/
		
	#xdg menu
	install -m 0644 pclinuxos-$(PACKAGE).desktop $(prefix)/$(datadir)/applications/pclinuxos-$(PACKAGE).desktop


clean:
	rm -f l10n/*.mo
	rm -f l10n/*.po.tmp
	rm -f pclinuxos-vit.desktop
	rm -f l10n/.intltool-merge-cache
	rm -f vit

cleandist:
	rm -rf $(PACKAGE)-$(ARCHIVEVER) $(PACKAGE)-$(VERSION).tar.bz2

distdir:
	if [ ! -d $(PACKAGE)-$(VERSION) ]; then mkdir $(PACKAGE)-$(VERSION); fi

localcopy: clean distdir
	tar cp --exclude=.svn --exclude=$(PACKAGE)-$(VERSION) --exclude=*.tar.bz2  . | tar xp -C $(PACKAGE)-$(VERSION)
	
tar: localcopy cleandist
	tar cjvf $(PACKAGE)-$(VERSION).tar.bz2 $(PACKAGE)-$(VERSION)
	rm -rf $(PACKAGE)-$(VERSION)

rpm: tar
	rpmbuild -ta $(PACKAGE)-$(VERSION).tar.bz2

rpm-install: rpm
	rpm -Uvh --force /usr/src/rpm/RPMS/noarch/$(PACKAGE)-$(VERSION)-$(RELEASE).noarch.rpm
